package com.shanzhu.volunteer.modules.volunteer.service;

import com.shanzhu.volunteer.common.entity.Response;
import com.shanzhu.volunteer.common.utils.EntityCopyUtil;
import com.shanzhu.volunteer.common.utils.SecurityUtil;
import com.shanzhu.volunteer.interfaces.body.volunteer.college.CollegeBody;
import com.shanzhu.volunteer.interfaces.vo.volunteer.college.CollegeVo;
import com.shanzhu.volunteer.modules.volunteer.entity.model.CeCollege;
import com.shanzhu.volunteer.modules.volunteer.repository.ICeCollegeRepo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 院校查询管理
 *
 * @author: ShanZhu
 * @date: 2023-04-01 15:07:20
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CeCollegeService {

    private final ICeCollegeRepo collegeRepo;

    /**
     * 请求院校列表
     *
     * @param collegeBody 学生请求体
     * @return 院校列表
     */
    public List<CeCollege> selectCollegeList(CollegeBody collegeBody) {
        List<CeCollege> ceCollegeList = collegeRepo.selectCollegeList(
                EntityCopyUtil.copyEntity(CeCollege.class, collegeBody)
        );
        // 设置录取率显示为百分比
        for (CeCollege ceCollege : ceCollegeList) {
            ceCollege.setAcceptanceRate(ceCollege.getAcceptanceRate() + "%");
        }
        return ceCollegeList;
    }

    /**
     * 添加院校
     *
     * @param collegeBody 院校请求体
     * @return 结果
     */
    public Response<Boolean> addCollege(CollegeBody collegeBody) {
        if (BooleanUtils.isFalse(uniqueCollegeNo(collegeBody.getCollegeNo(), null))) {
            return new Response<>().failMsg("保存院校失败,代码 '" + collegeBody.getCollegeNo() + "' 已存在");
        }
        CeCollege ceCollege = EntityCopyUtil.copyEntity(CeCollege.class, collegeBody);
        ceCollege.setCreatedUser(SecurityUtil.getUsername());
        collegeRepo.save(ceCollege);

        return new Response<>(Boolean.TRUE);
    }

    /**
     * 编辑院校
     *
     * @param collegeBody 院校请求体
     * @return 结果
     */
    public Response<Boolean> editCollege(CollegeBody collegeBody) {
        if (BooleanUtils.isFalse(uniqueCollegeNo(collegeBody.getCollegeNo(), collegeBody.getId()))) {
            return new Response<>().failMsg("编辑院校失败,代码 '" + collegeBody.getCollegeNo() + "' 已存在");
        }
        CeCollege ceCollege = EntityCopyUtil.copyEntity(CeCollege.class, collegeBody);
        ceCollege.setUpdatedUser(SecurityUtil.getUsername());
        collegeRepo.updateById(ceCollege);
        return new Response<>(Boolean.TRUE);
    }

    /**
     * 通过院校id查询院校信息
     *
     * @param collegeId 院校id
     * @return 结果
     */
    public CollegeVo getCollege(Integer collegeId) {
        return EntityCopyUtil.copyEntity(
                CollegeVo.class, collegeRepo.getById(collegeId)
        );
    }

    /**
     * 删除院校
     *
     * @param collegeIds 院校id列表
     * @return 结果
     */
    public Response<Boolean> deleteCollegeByIds(Integer[] collegeIds) {
        collegeRepo.removeByIds(Arrays.asList(collegeIds));
        return new Response<>(Boolean.TRUE);
    }

    /**
     * 判断院校代码是否存在
     *
     * @param collegeNo 院校编号
     * @param collegeId 院校id
     * @return 结果
     */
    private Boolean uniqueCollegeNo(String collegeNo, Integer collegeId) {
        CeCollege ceCollege = collegeRepo.selectCollegeByNo(collegeNo);
        if (ceCollege != null && !ceCollege.getId().equals(collegeId)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }


}

  